import { ILogin } from "../common/defInterface";
import lyu from "../framework/lyu";

class GRedis {
    private hasInit = false;
    private initCheck = [
        { key: `account:seedId`, value: 1000000 }, // 账号起始id
    ]
    async init() {
        for (const item of this.initCheck) {
            let key = item.key;
            let value = item.value;
            if (await lyu.redis(0).exists(key) == 0) {
                await lyu.redis(0).set(key, value);
            }
        }
        console.log("[Redis] redis启动完毕");
    }

    private getAccountSeedIdKey() {
        return `account:seedId`;
    }

    async getUserAccountSeedId() {
        let key = this.getAccountSeedIdKey();
        if (this.hasInit == false) {
            this.hasInit = true;
        }
        return await lyu.redis(0).incr(key);
    }

    private getUserAccountDataKey(account: string) {
        return `account:${account}`;
    }

    async getUserAccountData(account: string) {
        let key = this.getUserAccountDataKey(account);
        return await lyu.redis(0).getJson(key) as ILogin;
    }

    async setUserAccountData(account: string, userData: any) {
        let key = this.getUserAccountDataKey(account);
        await lyu.redis(0).setJson(key, userData, 24 * 60 * 60);
    }


}
let gRedis = new GRedis();
export default gRedis;